package com.hp.bon.sgw.wh.channel.invoices;

import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;

import org.simpleframework.http.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hp.bon.sgw.wh.channel.common.BizResult;
import com.hp.bon.sgw.wh.service.TMallResponseService;

public class Responsor {

	private static final Logger _log = LoggerFactory.getLogger(Responsor.class);
	
	private TMallResponseService tmallResponseService;
	
	public synchronized void setTMallResponseService(TMallResponseService tmallResponseService) {
		if(null == this.tmallResponseService) {
			this.tmallResponseService = tmallResponseService;
		}
	}

	public void doResponse(BizResult bizResult, Response response) {

		try {
			PrintStream body = response.getPrintStream();
			byte[] resps = bizResult.getHttpBody().getBytes("UTF-8");
			body.write(resps);
			body.close();
		} catch (Exception e) {
			_log.error(e.getMessage(), e);
		}

	}
	
	public void doExceptionResponse(Response response, String status, String failedCode, String failedReason) {
		try {
			PrintStream body = response.getPrintStream();
			Map<String, String> data = new HashMap<String, String>();
			data.put("tbOrderNo", "");
			data.put("coopOrderNo", "");
			data.put("coopOrderStatus", status);
			data.put("coopOrderSnap", "ReCharge");
			data.put("coopOrderSuccessTime", "");
			data.put("failedCode", failedCode);
			data.put("failedReason", failedReason);
			String errorBody = tmallResponseService.getResponse(data);
			body.write(errorBody.getBytes("GBK"));
			body.close();
		} catch (Exception e) {
			_log.error("TMallChannel errorAck error, msg = {}", e.getMessage());
            _log.error("",e);
		}
	}
//	
//	public void doExceptionResponse(BizResult bizResult, Response response) {
//
//		try {
//			PrintStream body = response.getPrintStream();
//			byte[] resps = bizResult.getHttpBody().getBytes("UTF-8");
//			body.write(resps);
//			body.close();
//		} catch (Exception e) {
//			_log.error(e.getMessage(), e);
//		}
//
//	}
//	

}
